using UnityEngine;
using UnityEngine.UI;

namespace Assets.Scripts.GDK
{
	public abstract class gdk_baseLayer<T> where T : gdk_baseLayer<T>, new()
	{
		private static T _instance;

		private GameObject node;

		private GameObject mask;

		private int childCount;

		private GameObject[] items;

		public static T Instance
		{
			get
			{
				if (_instance == null)
				{
					_instance = new T();
				}
				return _instance;
			}
		}

		public virtual bool isMask()
		{
			return false;
		}

		public virtual void setNode(GameObject _node)
		{
			node = _node;
			childCount = 0;
			if (isMask())
			{
				initMask();
			}
		}

		public virtual GameObject getNode()
		{
			return node;
		}

		private void initMask()
		{
			mask = gdk_utils.NewImage().gameObject;
			mask.name = node.name + "[mask]";
			mask.SetActive(value: false);
			RectTransform component = mask.GetComponent<RectTransform>();
			component.sizeDelta = gdk_utils.getSceneSize() * new Vector3(2f, 2f, 0f);
			mask.GetComponent<Image>().color = new Color(0f, 0f, 0f, 25f / 51f);
			component.SetParent(node.transform);
			component.localPosition = new Vector3(0f, 0f, 1f);
			component.localScale = Vector3.one;
		}

		public virtual void show(GameObject item)
		{
			_show(item);
		}

		public virtual void hide(GameObject item)
		{
			_hide(item);
		}

		private void _show(GameObject item)
		{
			childCount++;
			item.SetActive(value: true);
			item.transform.SetParent(node.transform, worldPositionStays: false);
			checkMaskLayer();
		}

		private void _hide(GameObject item)
		{
			childCount--;
			UnityEngine.Object.Destroy(item);
			checkMaskLayer();
		}

		private void checkMaskLayer()
		{
			if (isMask())
			{
				if (childCount > 0)
				{
					mask.SetActive(value: true);
				}
				else
				{
					mask.SetActive(value: false);
				}
			}
		}
	}
}
